package com.huilian.iotbox.admin.controller;

import com.huilian.iotbox.admin.service.StmtResultService;
import com.huilian.iotbox.admin.service.UserService;
import com.huilian.iotbox.data.common.ServerResponse;
import com.huilian.iotbox.data.controller.BaseController;
import com.huilian.iotbox.data.dto.StmtResultDto;
import com.huilian.iotbox.data.enums.RoleEnum;
import com.huilian.iotbox.data.vo.UserInfo;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

/**
 * @author: loong
 * @create: 2021-09-29 15:57
 **/
@RestController
@RequestMapping("/api/stmtResult")
public class StmtResultController extends BaseController {
    @Resource
    private StmtResultService stmtResultService;
    @Resource
    private UserService userService;

    @PostMapping("/findStmtResult")
    public ServerResponse findStmtResult(@RequestBody StmtResultDto stmtResultDto, HttpServletRequest request) {
        String pageNum = request.getParameter("pageNum");
        String pageSize = request.getParameter("pageSize");
        UserInfo userInfo = userService.getLoginUserInfo(request);
        stmtResultDto.setUserInfo(userInfo);
        stmtResultDto.setPageNum(Integer.parseInt(pageNum));
        stmtResultDto.setPageSize(Integer.parseInt(pageSize));
        if (userInfo.getRoleId().compareTo(RoleEnum.SUB_ACCOUNT.getRoleId()) == 0) {
            stmtResultDto.setId(userInfo.getParentId());
        } else {
            stmtResultDto.setId(userInfo.getId());
        }
        return ServerResponse.createBySuccess(stmtResultService.findStmtResultDto(stmtResultDto));
    }

    @PostMapping("/exportTheOrder")
    public ServerResponse exportTheOrder(@RequestBody StmtResultDto stmtResultDto, HttpServletRequest request) throws ParseException {
        UserInfo userInfo = userService.getLoginUserInfo(request);
        stmtResultDto.setUserInfo(userInfo);
        if(stmtResultDto.getPayDateStart()==null){
            return ServerResponse.createByErrorMessage("请求异常，导出时间间隔建议在两个月内");
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSX");
        sdf.setTimeZone(TimeZone.getTimeZone("GMT+8")); // 设置时区为东八区
        stmtResultDto.setCreateTime(sdf.parse(stmtResultDto.getPayDateStart()));
        stmtResultDto.setUpdateTime(sdf.parse(stmtResultDto.getPayDateEnd()));
        boolean flag = compareReqTimeSectionMonth(stmtResultDto.getCreateTime(),stmtResultDto.getUpdateTime());
        if(!flag){
            return ServerResponse.createByErrorMessage("请求异常，导出时间间隔建议在两个月内");
        }
        return stmtResultService.exportTheOrder(stmtResultDto);
    }
    private boolean compareReqTimeSectionMonth(Date beginTime, Date endTime) {
        Calendar calendar = Calendar.getInstance(); // 得到日历
        if (beginTime == null) {
            return false;
        }
        calendar.setTime(beginTime); // 把当前时间赋给日历
        calendar.add(Calendar.MONTH, 2); // 两个月
        beginTime = calendar.getTime();
        int i = beginTime.compareTo(endTime);
        return i >= 0;
    }
}
